package handler

import (
	"context"
	"rpc/global"
	"rpc/models"
	__ "rpc/proto"
)

type Server struct {
	__.UnimplementedUserServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) List(_ context.Context, in *__.ListRequest) (*__.ListResponse, error) {
	var list []models.Orders
	global.DB.Find(&list).Where("name = ?", "%"+in.Name+"%")
	var resp []*__.List
	for _, v := range list {
		resp = append(resp, &__.List{
			Name:      v.Name,
			Password:  v.Password,
			GoodName:  v.GoodName,
			GoodPrice: float32(v.GoodPrice),
			PayStatus: v.PayStatus,
		})
		resp = append(resp, &__.List{})
	}

	return &__.ListResponse{
		List: resp,
	}, nil
}
func (s *Server) Add(_ context.Context, in *__.AddRequest) (*__.AddResponse, error) {
	add := models.Orders{
		Name:      in.Name,
		Password:  in.Password,
		GoodName:  in.GoodName,
		GoodPrice: float64(in.GoodPrice),
		PayStatus: in.PayStatus,
	}
	global.DB.Create(&add)

	return &__.AddResponse{
		Id: add.Id,
	}, nil
}
